import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:nft_shop/login/controller/logic_controller.dart';
import 'package:nft_shop/login/page/login_register_page.dart';
import 'package:nft_shop/login/page/login_reset_password.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/sift/index/index.dart';
import 'package:nft_shop/widgets/load_image.dart';
import 'package:nft_shop/widgets/my_app_bar.dart';
import 'package:nft_shop/widgets/my_button.dart';
import 'package:nft_shop/widgets/my_scroll_view.dart';
import 'package:nft_shop/widgets/my_text_field.dart';

class LoginPage extends StatefulWidget {
  bool? backHome;

  LoginPage({Key? key, this.backHome}) : super(key: key);

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  //定义一个controller
  final TextEditingController _phoneController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();

  final FocusNode _nodeText1 = FocusNode();
  final FocusNode _nodeText2 = FocusNode();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    LoginLogic.to;
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    EasyLoading.dismiss();
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: (){FocusScope.of(context).requestFocus(new FocusNode());},
      child: Scaffold(
        appBar: const MyAppBar(
          backgroundColor: Colors.transparent,
        ),
        body: MyScrollView(
          padding: const EdgeInsets.only(left: 20.0, right: 20.0, top: 17.0),
          children: <Widget>[
            Center(
                child: LoadAssetImage(
                  "logo",
                  width: 107,
                  height: 104,
                )),
            SizedBox(height: 37),
            Center(
              child: Text(
                // "登录${Global.AppName}商城",
                "元界数藏",
                style: TextStyle(
                  fontSize: 26,
                  color: Global.kTheme.textPrimary1,
                  fontFamily: Global.mySemiboldFont,),
              ),
            ),
            SizedBox(height: 8),
            // Text(
            //   "您好，欢迎来到${Global.AppName}商城",
            //   style: TextStyle(
            //       fontSize: 18,
            //       color: Global.kTheme.textPrimary10,),
            // ),
            // SizedBox(height: 29),
            Container(
              width: Get.width-20,
              child:  Row(
                children: [
                  Padding(padding: EdgeInsets.only(right: 10),child: Text(
                    "手机号",
                    style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
                  ),),
                  Expanded(
                    child: Row(children: [Expanded(child: MyTextField(
                      key: const Key('phone'),
                      focusNode: _nodeText1,
                      controller: _phoneController,
                      keyboardType: TextInputType.phone,
                      hintText: "请输入手机号",
                    ))],),
                  ),
                ],
              ),
            ),
            SizedBox(height: 12),
            Container(
              child: Row(
                children: [
                  Padding(padding: EdgeInsets.only(right: 10),child: Text(
                    "密   码",
                    style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
                  )),
                 Expanded(child: Row(children: [Expanded(child:  MyTextField(
                   keyName: 'password',
                   isInputPwd: true,
                   focusNode: _nodeText2,
                   controller: _passwordController,
                   keyboardType: TextInputType.visiblePassword,
                   hintText: "请输入密码",
                 ))],)),
                ],
              ),
            ),
            SizedBox(height: 42),
            Container(
              height: 40,
              decoration: BoxDecoration(
                color: Colors.white,
                // Colours.app_main,
                borderRadius: BorderRadius.circular(5),
              ),
              child: MyButton(
                key: const Key('login'),
                minHeight: 40,
                onPressed: () async {
                  _nodeText1.unfocus();
                  _nodeText2.unfocus();

                  if (_phoneController.text.length == 0) {
                    EasyLoading.showToast('请输入手机号');
                    return;
                  }
                  if (_passwordController.text.length == 0) {
                    EasyLoading.showToast('请输入密码');
                    return;
                  }
                  LoginLogic.to.login(username: _phoneController.text, password: _passwordController.text);
                },
                text: "登录",
                fontSize: 17,
                textColor: Global.kTheme.textPrimary5,
                backgroundColor: Global.kTheme.colorPrimary,
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(
                    alignment: Alignment.center,
                    child: GestureDetector(
                      child: Text.rich(TextSpan(
                        children: [
                          TextSpan(
                              text: "还没有账号？",
                              style: TextStyle(color: Global.kTheme.inputHint1, fontSize: 12)),
                          TextSpan(
                              text: "立即注册",
                              style: TextStyle(color: Global.kTheme.textPrimary4, fontSize: 12)),
                        ],
                      )),
                      onTap: () => Get.to(LoginRegisterPage()),
                    )),
                TextButton(
                    onPressed: () => Get.to(LoginResetPassWordPage()),
                    child: Text(
                      "忘记密码？",
                      style: TextStyle(
                        fontSize: 12,
                        fontFamily: Global.myRegularFont,
                        color: Global.kTheme.inputHint1,),
                    ),
                    style: ButtonStyle(
                        padding: MaterialStateProperty.all(
                            EdgeInsetsDirectional.all(0)))),
              ],
            )
          ],
        ),
      ),
    );
  }
}
